//
//  LXApp+Wifi.swift
//  LXApp
//
//  Created by yoctech on 2021/8/18.
//

import Foundation
import SystemConfiguration.CaptiveNetwork

public class LXWifiInfo: NSObject {
    public var ssid: String
    public var mac: String
    
    public init(ssid: String, mac: String) {
        self.ssid = ssid
        self.mac = mac
    }
    
    public override var description: String {
        return "wifi ssid => \(ssid), mac => \(mac)"
    }
}

public extension LXApp {
    /// wifi信息, iOS12需要配置WiFi权限, iOS13.2需要先请求定位
    static var wifiInfo: LXWifiInfo? {
        guard let unwrappedCFArrayInterfaces = CNCopySupportedInterfaces() else {
                    return nil
        }
        guard let swiftInterfaces = (unwrappedCFArrayInterfaces as NSArray) as? [String] else {
            return nil
        }
        for interface in swiftInterfaces {
            guard let unwrappedCFDictionaryForInterface = CNCopyCurrentNetworkInfo(interface as CFString) else {
                return nil
            }
            guard let SSIDDict = (unwrappedCFDictionaryForInterface as NSDictionary) as? [String: AnyObject] else {
                return nil
            }
            if let ssid = SSIDDict["SSID"] as? String,
                let bssid = SSIDDict["BSSID"] as? String {
                return LXWifiInfo.init(ssid: ssid, mac: bssid)
            }
        }
        return nil
    }
}
